package com.ljw.store.dao;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.ljw.store.entity.ItemType;
import com.login.gwj.domain.Admin;



public class AdminDaoImp implements AdminDao {

	private SessionFactory sessionFactory;
	int maxtypename;
	@Resource
	private HibernateTemplate hibernateTemplate = null;
	
	private Query query;
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory = sessionFactory;
	}
	public Admin updateAdmin(Admin admin) {
			sessionFactory.getCurrentSession().merge(admin);
			return admin;
	}

	public List<Admin> searchStore(Admin admin) {
		query = sessionFactory.getCurrentSession().createQuery(
		"from Admin where adminId=?");
		query.setInteger(0, admin.getId());
		List<Admin> adminList = query.list();
		System.out.println(adminList.size());
		return adminList;
	}
	public List<ItemType> alltype() {
		// TODO Auto-generated method stub
		query = sessionFactory.getCurrentSession().createQuery("from ItemType");
		List<ItemType> List = query.list();		
		return List;
	}
	public void addtype(String typename) {
		// TODO Auto-generated method stub
		query=sessionFactory.getCurrentSession().createQuery("select Max(Itemname) FROM ItemType");	
		if((Integer)query.uniqueResult()==null){
			maxtypename = 0;
		}
		else maxtypename = (Integer) query.uniqueResult();
		ItemType IT=new ItemType();
		IT.setItemTypeName(typename);
		IT.setItemname(++maxtypename);
		sessionFactory.getCurrentSession().persist(IT);
		
	}
	public void deteletype(String Itemname) {
		// TODO Auto-generated method stub
		
		sessionFactory.getCurrentSession().createQuery("delete from ItemType where Itemname="+Itemname).executeUpdate();  
	}
	public List<ItemType> alltypeBYid(String itemTypeId) {
		// TODO Auto-generated method stub
		query = sessionFactory.getCurrentSession().createQuery("from ItemType where itemTypeId=?");
		query.setString(0, itemTypeId);
		List<ItemType> List = query.list();		
		return List;
	}
	public void updatetype(String typename, String itemTypeId) {
		// TODO Auto-generated method stub
		query = sessionFactory.getCurrentSession().createQuery("from ItemType where itemTypeId=?");
		query.setString(0, itemTypeId);
		List<ItemType> List = query.list();	
		ItemType IT=new ItemType();
		IT = List.get(0);
		IT.setItemTypeName(typename);
		sessionFactory.getCurrentSession().update(IT);
	}
}
